﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using IPR_StudyMeetingMrgSys.DBUtility;//Please add references
namespace IPR_StudyMeetingMrgSys.DAL
{
	/// <summary>
	/// 数据访问类:MessageInfo
	/// </summary>
	public partial class MessageInfo
	{
		public MessageInfo()
		{}
		#region  BasicMethod



		/// <summary>
		/// 增加一条数据
		/// </summary>
		public long Add(IPR_StudyMeetingMrgSys.Model.MessageInfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into MessageInfo(");
			strSql.Append("MsgID,MsgTitle,MsgContent,MsgAddTime,DepID,MsgStartTime,MsgEndTime,MsgType,CurDepID,CurDepName)");
			strSql.Append(" values (");
			strSql.Append("@MsgID,@MsgTitle,@MsgContent,@MsgAddTime,@DepID,@MsgStartTime,@MsgEndTime,@MsgType,@CurDepID,@CurDepName)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@MsgID", SqlDbType.BigInt,8),
					new SqlParameter("@MsgTitle", SqlDbType.NVarChar,20),
					new SqlParameter("@MsgContent", SqlDbType.NVarChar,1024),
					new SqlParameter("@MsgAddTime", SqlDbType.DateTime),
					new SqlParameter("@DepID", SqlDbType.NVarChar,200),
					new SqlParameter("@MsgStartTime", SqlDbType.DateTime),
					new SqlParameter("@MsgEndTime", SqlDbType.DateTime),
                    new SqlParameter("@MsgType", SqlDbType.NVarChar,100),
                    new SqlParameter("@CurDepID", SqlDbType.Int,4),
                    new SqlParameter("@CurDepName", SqlDbType.NVarChar,20)};
			parameters[0].Value = model.MsgID;
			parameters[1].Value = model.MsgTitle;
			parameters[2].Value = model.MsgContent;
			parameters[3].Value = model.MsgAddTime;
			parameters[4].Value = model.DepID;
			parameters[5].Value = model.MsgStartTime;
            parameters[6].Value = model.MsgEndTime;
            parameters[7].Value = model.MsgType;
            parameters[8].Value = model.CurDepID;
            parameters[9].Value = model.CurDepName;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt64(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(IPR_StudyMeetingMrgSys.Model.MessageInfo model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update MessageInfo set ");
			strSql.Append("MsgID=@MsgID,");
			strSql.Append("MsgTitle=@MsgTitle,");
			strSql.Append("MsgContent=@MsgContent,");
			strSql.Append("MsgAddTime=@MsgAddTime,");
			strSql.Append("DepID=@DepID,");
			strSql.Append("MsgStartTime=@MsgStartTime,");
            strSql.Append("MsgEndTime=@MsgEndTime,");
            strSql.Append("MsgType=@MsgType,"); 
            strSql.Append("CurDepID=@CurDepID,"); 
            strSql.Append("CurDepName=@CurDepName");
            strSql.Append(" where AutoID=@AutoID");
			SqlParameter[] parameters = {
					new SqlParameter("@MsgID", SqlDbType.BigInt,8),
					new SqlParameter("@MsgTitle", SqlDbType.NVarChar,20),
					new SqlParameter("@MsgContent", SqlDbType.NVarChar,1024),
					new SqlParameter("@MsgAddTime", SqlDbType.DateTime),
					new SqlParameter("@DepID", SqlDbType.NVarChar,200),
					new SqlParameter("@MsgStartTime", SqlDbType.DateTime),
					new SqlParameter("@MsgEndTime", SqlDbType.DateTime),
					new SqlParameter("@AutoID", SqlDbType.BigInt,8),
                    new SqlParameter("@MsgType", SqlDbType.NVarChar,100),
                    new SqlParameter("@CurDepID", SqlDbType.Int,4),
                    new SqlParameter("@CurDepName", SqlDbType.NVarChar,20)};
			parameters[0].Value = model.MsgID;
			parameters[1].Value = model.MsgTitle;
			parameters[2].Value = model.MsgContent;
			parameters[3].Value = model.MsgAddTime;
			parameters[4].Value = model.DepID;
			parameters[5].Value = model.MsgStartTime;
			parameters[6].Value = model.MsgEndTime;
            parameters[7].Value = model.AutoID;
            parameters[8].Value = model.MsgType;
            parameters[9].Value = model.CurDepID;
            parameters[10].Value = model.CurDepName;

            int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(long MsgID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from MessageInfo ");
			strSql.Append(" where MsgID=@MsgID");
			SqlParameter[] parameters = {
					new SqlParameter("@MsgID", SqlDbType.BigInt)
			};
			parameters[0].Value = MsgID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string AutoIDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from MessageInfo ");
			strSql.Append(" where AutoID in ("+AutoIDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public IPR_StudyMeetingMrgSys.Model.MessageInfo GetModel(long MsgID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 AutoID,MsgID,MsgTitle,MsgContent,MsgAddTime,DepID,MsgStartTime,MsgEndTime,MsgType,dbo.GetDepName(DepID) as DepName,CurDepID,CurDepName from MessageInfo ");
			strSql.Append(" where MsgID=@MsgID");
			SqlParameter[] parameters = {
					new SqlParameter("@MsgID", SqlDbType.BigInt)
			};
			parameters[0].Value = MsgID;

			IPR_StudyMeetingMrgSys.Model.MessageInfo model=new IPR_StudyMeetingMrgSys.Model.MessageInfo();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public IPR_StudyMeetingMrgSys.Model.MessageInfo DataRowToModel(DataRow row)
		{
			IPR_StudyMeetingMrgSys.Model.MessageInfo model=new IPR_StudyMeetingMrgSys.Model.MessageInfo();
			if (row != null)
			{
				if(row["AutoID"]!=null && row["AutoID"].ToString()!="")
				{
					model.AutoID=long.Parse(row["AutoID"].ToString());
				}
				if(row["MsgID"]!=null && row["MsgID"].ToString()!="")
				{
					model.MsgID=long.Parse(row["MsgID"].ToString());
				}
				if(row["MsgTitle"]!=null)
				{
					model.MsgTitle=row["MsgTitle"].ToString();
				}
				if(row["MsgContent"]!=null)
				{
					model.MsgContent=row["MsgContent"].ToString();
				}
				if(row["MsgAddTime"]!=null && row["MsgAddTime"].ToString()!="")
				{
					model.MsgAddTime=DateTime.Parse(row["MsgAddTime"].ToString());
				}
				if(row["DepID"]!=null)
				{
					model.DepID=row["DepID"].ToString();
				}
				if(row["MsgStartTime"]!=null && row["MsgStartTime"].ToString()!="")
				{
					model.MsgStartTime=DateTime.Parse(row["MsgStartTime"].ToString());
				}
				if(row["MsgEndTime"]!=null && row["MsgEndTime"].ToString()!="")
				{
					model.MsgEndTime=DateTime.Parse(row["MsgEndTime"].ToString());
                }
                if (row["MsgType"] != null)
                {
                    model.MsgType = row["MsgType"].ToString();
                }
                if (row["CurDepID"] != null && row["CurDepID"].ToString() != "")
                {
                    model.CurDepID = int.Parse(row["CurDepID"].ToString());
                }
                if (row["CurDepName"] != null)
                {
                    model.CurDepName = row["CurDepName"].ToString();
                }
            }
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select AutoID,MsgID,MsgTitle,MsgContent,MsgAddTime,DepID,MsgStartTime,MsgEndTime,MsgType,dbo.GetDepName(DepID) as DepName,CurDepID,CurDepName ");
			strSql.Append(" FROM MessageInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" AutoID,MsgID,MsgTitle,MsgContent,MsgAddTime,DepID,MsgStartTime,MsgEndTime,MsgType,dbo.GetDepName(DepID) as DepName,CurDepID,CurDepName ");
			strSql.Append(" FROM MessageInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM MessageInfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.AutoID desc");
			}
			strSql.Append(")AS Row, T.*  from MessageInfo T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

        /*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "MessageInfo";
			parameters[1].Value = "AutoID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

        #endregion  BasicMethod
        #region  ExtensionMethod
            /// <summary>
            /// 获取本部门 最后一条消息
            /// </summary>
            /// <returns></returns>
        public DataSet GetSingleInfo(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 AutoID,MsgID,MsgTitle,MsgContent,MsgAddTime,DepID,MsgStartTime,MsgEndTime,MsgType,dbo.GetDepName(DepID) as DepName,CurDepID,CurDepName ");
            strSql.Append(" FROM MessageInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        #endregion  ExtensionMethod
    }
}

